<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SpecialAjaxController
 *
 * @author Ganda
 */
class SpecialAjaxController extends Controller {

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('checklist', 'requestCurrentURL', 'updateexpired', 'getLocationCompany'),
                'users' => array('@'),
            ),
        );
    }

    private function getAllowMyAccessRule($seachAction) {
        $boleh = FALSE;
        $myAccessRule = $this->accessRules();

        for ($i = 0; $i < count($myAccessRule); $i++) {
            $arr = $myAccessRule[$i];
            $arrAction = array();
            $arrUser = array();

            if (is_int(array_search('allow', $arr))) {
                $arrAction = isset($arr['actions']) ? $arr['actions'] : array();
                $arrUser = isset($arr['users']) ? $arr['users'] : array();

                if (is_int(array_search($seachAction, $arrAction))) {
                    if (is_int(array_search(Yii::app()->user->name, $arrUser)) || is_int(array_search(!Yii::app()->user->isGuest ? '@' : '', $arrUser)) || is_int(array_search(Yii::app()->user->isGuest ? '*' : '', $arrUser))
                    ) {
                        $boleh = TRUE;
                    }
                }
            }
        }

        return $boleh;
    }

    public function actionChecklist() {
        $accRule = Sysappaccessrule::model()->findAll("menugroupid='{$_POST['menuGroupID']}' and PermissionAccessID = '1' and RuleTypeID = '1' and isActive = '1'");

        $a = array();

        if ($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))) {
            foreach ($accRule as $ar) {
                array_push($a, array('ControllerName' => $ar->ControllerName, 'RuleValues' => $ar->RuleValues));
            }
        }
        echo json_encode($a);
    }

    public function actionRequestCurrentURL() {
        $cr = $_POST['cr'];
        $co = $_POST['co'];
        $ca = $_POST['ca'];
        $unauthorizedURL = Appglobalparameters::model()->find("parametername='UNAUTHORIZED' and isactive = '1'");
        $videoNotFoundURL = Appglobalparameters::model()->find("parametername='VIDEO_NOT_FOUND' and isactive = '1'");

        if (isset($cr) && isset($co) && isset($ca)) {
            $reqvid = Cctvrequestvideo::model()->find("credentialname='{$cr}' and computername = '{$co}' and cameraname = '{$ca}' and isactive = '1'");

            if (!isset($reqvid)) {
                echo json_encode(isset($videoNotFoundURL) ? $videoNotFoundURL->value : '');
            } else {
                if ($cr == Yii::app()->user->name) {
                    $usrs = new Sysusrsecurity;
                    $today = Date('Y-m-d');
                    $usrs = Sysusrsecurity::model()->find("credentialname='{$cr}' and islocked = '0' and expireddate > '{$today}'", array('credentialname ASD'));

                    $mappVid = new Cctvmappingcamera();
                    $mappVid = Cctvmappingcamera::model()->find("menugroupid = '{$usrs->menugroupid}' and computername = '{$co}' and cameraname = '{$ca}' and isactive = '1'");
                    if (isset($mappVid)) {
                        echo json_encode($reqvid->requesturl);
                    } else {
                        echo json_encode(isset($unauthorizedURL) ? $unauthorizedURL->value : '');
                    }
                } else {
                    echo json_encode(isset($unauthorizedURL) ? $unauthorizedURL->value : '');
                }
            }
        }
    }

    public function actionUpdateexpired() {
        if (isset($_POST['cr']) && $_POST['co'] && $_POST['ca']) {
            $query = "UPDATE cctvrequestvideo SET requestexpired='" . date("Y-m-d H:i:s", mktime(date('H'), date('i') + 2, date('s'), date('m'), date('d'), date('Y'))) . "' where credentialname='{$_POST['cr']}' and computername = '{$_POST['co']}' and cameraname = '{$_POST['ca']}' and isactive = '1'";
            $command = Yii::app()->db->createCommand($query);
            $command->execute();
            /* $req = new Cctvrequestvideo;
              $req = Cctvrequestvideo::model()->find("credentialname='{$_POST['cr']}' and computername = '{$_POST['co']}' and cameraname = '{$_POST['ca']}' and isactive = '1'");
              $req->requestexpired = date("Y-m-d H:i:s", mktime(date('H'), date('i') + 2, date('s'), date('m'), date('d'), date('Y')));

              $test = Cctvrequestvideo::model()->find("credentialname='{$_POST['cr']}' and computername = '{$_POST['co']}' and cameraname = '{$_POST['ca']}' and isactive = '1'")->requestid;
              if($req->save()){
              echo 'sukses';
              }else{
              echo 'tidak'.$test;
              } */
        }
    }

    public function actionGetLocationCompany() {
        $getCompGroup = Sysusrsecurity::model()->find("credentialname='" . $_GET['username'] . "'")->menugroupid;

        $sql = "select distinct regency.regencyname, regency.latitude, regency.longitude from sysappmenugroup groupapp
JOIN invcompany inv ON groupapp.companyID = inv.companyid
JOIN invcompanybranch branch ON branch.companyID = inv.companyid
JOIN gflocation gloc ON gloc.locationid = branch.addresslocationid
JOIN gfregency regency ON regency.regencyid = gloc.regencyid 
where groupapp.menugroupid=" . $getCompGroup . " and groupapp.isactive='1'
";

        $sql2 = "select gloc.locationid,branch.companybranchname, gloc.latitude, gloc.longitude from sysappmenugroup groupapp
JOIN invcompany inv ON groupapp.companyid = inv.companyid
JOIN invcompanybranch branch ON branch.companyid = inv.companyid
JOIN gflocation gloc ON gloc.locationid = branch.addresslocationid
JOIN gfregency regency ON regency.regencyid = gloc.regencyid 
where groupapp.menugroupid=" . $getCompGroup . " and groupapp.isactive='1'";
        $getDataSDMOne = Yii::app()->db->createCommand($sql)->queryAll();
        $getDataSDMTwo = Yii::app()->db->createCommand($sql2)->queryAll();
        $data = array();
        foreach ($getDataSDMOne as $getAllOne) {
            $data['one'][] = $getAllOne;
        }
        foreach ($getDataSDMTwo as $getAllTwo) {
            $data['two'][] = $getAllTwo;
        }
        echo json_encode($data);
    }
    public function actionGetDataCctv() {
        
    }

}

?>
